using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using Microsoft.Xna.Framework;
using CreamXStudio;
using CreamXStudio.Classes;
using CreamX.Framework.Game;
using CreamX.Framework.SceneGraph;


namespace CreamXStudio.Screens
{
    public partial class ObjectEditor : EditorControlBase
    {
        public CreamXSceneData _data;
        public ObjectEditor()
        {
            if(SessionData.Instance.CurrentScene!=null &&
                SessionData.Instance.CurrentScene.SceneData!=null)
                _data = SessionData.Instance.CurrentScene.SceneData;

            InitializeComponent();
        }
        internal override void SetData(object data)
        {
            SelectedObject= (CreamXObject)data;
            RefreshMaterials();
            base.SetData(data);
        }

        public bool IsScenery=false;
        public void RefreshMaterials()
        {
            cboMaterial.Items.Clear();
            if (_data == null)
                return;
            foreach (CreamXMaterialBase __m in _data.Materials)
            {
                cboMaterial.Items.Add(__m.Name);
            }
            if (_selectedObject!=null)
            {
                if(_selectedObject.Material!=null)
                    cboMaterial.Text = _selectedObject.Material.Name;
            }
        }
        
        private CreamXObject _selectedObject;

        public CreamXObject SelectedObject
        {
            get { return _selectedObject; }
            set
            {
                _selectedObject = value;
                if (SelectedObject != null)
                {
                    trackRotation.Value = (int)MathHelper.ToDegrees(SelectedObject.Rotation);
                    trackScale.Value =(int)MathHelper.Clamp( (int)(SelectedObject.Scale * 10),0,trackScale.Maximum);
                    if (_selectedObject!=null)
                    {
                        if (_selectedObject.Material != null)
                        {
                            cboMaterial.Text = _selectedObject.Material.Name;
                            pictureBox1.Image = SessionData.Instance.ImageCache[_selectedObject.Material.Name];

                            //_selectedObject.Origin = new Vector2(pictureBox1.Image.Width / 2,
                              //  pictureBox1.Image.Height / 2);
                        }
                        else
                        {
                            cboMaterial.Text = "<Please Select>";
                            _selectedObject.Origin = Vector2.Zero;
                        }
                        numLayer.Value = _selectedObject.Layer;
                        numPosX.Value = (int)_selectedObject.Position.X;
                        numPosY.Value = (int)_selectedObject.Position.Y;
                        numOriginY.Value = (int)_selectedObject.Origin.Y;
                        numOriginX.Value = (int)_selectedObject.Origin.X;
                        txtName.Text = _selectedObject.Name;
                        RefreshMaterials();
                       
                    }
                    
                }
            }
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            if(SelectedObject!=null)
                SelectedObject.Rotation = MathHelper.ToRadians(trackRotation.Value);
        }

        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            if (SelectedObject != null)
                SelectedObject.Scale= (float)trackScale.Value/10f;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            _selectedObject.Material = _data.GetMaterial(cboMaterial.Text);
            pictureBox1.Image = SessionData.Instance.ImageCache[_selectedObject.Material.Name];
            
        }
         protected override void OnPaint(PaintEventArgs e)
        {
            
            base.OnPaint(e);
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            _selectedObject.Layer = (int)numLayer.Value;
        }

        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {
            _selectedObject.SetPosition((int)numPosX.Value, _selectedObject.Position.Y);
        }

        private void ObjectEditor_Load(object sender, EventArgs e)
        {

        }

        private void numericUpDown3_ValueChanged(object sender, EventArgs e)
        {
            _selectedObject.SetPosition(_selectedObject.Position.X, (int)numPosY.Value);
        }

        private void numOriginX_ValueChanged(object sender, EventArgs e)
        {
            _selectedObject.Origin=new Vector2((float)numOriginX.Value, _selectedObject.Origin.Y);
        }

        private void numOriginY_ValueChanged(object sender, EventArgs e)
        {
            _selectedObject.Origin = new Vector2(_selectedObject.Origin.X, (float)numOriginY.Value);
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            _selectedObject.Name = txtName.Text ;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (_selectedObject.Material != null)
            {
                numOriginY.Value = (int)_selectedObject.Material.Texture.Height/ 2;
                numOriginX.Value = (int)_selectedObject.Material.Texture.Width / 2;
            }
        }
    }
}
